//
//  FQAuthButtonView.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/22.
//

import UIKit

class FQAuthBottonView: UIView {
    let selectBtn = QMUIButton()
    let completionBtn = QMUIButton()
    
    var buttonBlock : (()->Void)?
    var toggleBlock : ((_ isSelected : Bool)->Void)?
    convenience init() {
        self.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 90))
        
        backgroundColor = .white
        
        selectBtn.setImage(UIImage(named: "icon_select_off"), for: .normal)
        selectBtn.setImage(UIImage(named: "icon_select_on"), for: .selected)
        selectBtn.isSelected = true
        selectBtn.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        selectBtn.tag = 1
        addSubview(selectBtn)
        
        selectBtn.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(5)
            make.size.equalTo(20)
        }
        
        let titleLb = QMUILabel(font: .mSFont(13), textColor: .color51)
        addSubview(titleLb)
        
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(selectBtn.snp.right).offset(5)
            make.centerY.equalTo(selectBtn)
        }
        
        let titleStr : ASAttributedString = """
            请仔细阅读并同意 \("《用户隐私协议》", .foreground(.colorMain), .action { [unowned self] in
                let vc = BaseWebViewController(.local("agreement02", "html"))
                self.qmui_viewController?.navigationController?.qmui_pushViewController(vc, animated: true)
            })
            """
        titleLb.attributed.text = titleStr
        
        completionBtn.setBackgroundImage(.qmui_image(with: .colorMain), for: .normal)
        completionBtn.setBackgroundImage(.qmui_image(with: .qmui_color(withHexString: "d1d2d6")), for: .disabled)
        completionBtn.clipsToBounds = true
        completionBtn.layer.cornerRadius = 25
        completionBtn.isEnabled = false
        completionBtn.setTitleColor(.white, for: .normal)
        completionBtn.setTitle("完成，开始人脸识别", for: .normal)
        completionBtn.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        completionBtn.tag = 2
        addSubview(completionBtn)
        
        completionBtn.snp.makeConstraints { make in
            make.top.equalTo(selectBtn.snp.bottom).offset(12)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(50)
        }
    }
    
    @objc func buttonClick(_ button : QMUIButton) {
        switch button.tag {
        case 1:
            button.isSelected.toggle()
            if toggleBlock != nil {
                toggleBlock!(button.isSelected)
            }
        default:
            if buttonBlock != nil {
                buttonBlock!()
            }
        }
    }
}
